package branice.spring.cloud.config;

import branice.spring.cloud.api.API2;
import branice.spring.cloud.api.API3;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//@Configuration
public class SoapClient {

    // WebService 服务地址（需替换为实际地址）
    private static final String SERVICE_URL = "http://127.0.0.1:8083/apiws";
    private static final String SERVICE_URL_1 = "http://127.0.0.1:8083/apiws3";

    @Bean
    public API2 api2Client() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(API2.class);
        factory.setAddress(SERVICE_URL);
        API2 client = (API2) factory.create();
        return client;
    }


    @Bean
    public API3 api3Client() {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(API2.class);
        factory.setAddress(SERVICE_URL);
        API3 client = (API3) factory.create();
        return client;
    }

    /**
     * 为 SOAP 客户端设置超时时间
     */
    private void configureTimeout(API2 client) {
        // 获取 CXF 客户端代理
        Client proxy = ClientProxy.getClient(client);

        if (proxy != null) {
            // 获取 HTTP 通道
            HTTPConduit conduit = (HTTPConduit) proxy.getConduit();

            // 创建 HTTP 策略
            HTTPClientPolicy policy = new HTTPClientPolicy();

            // 设置超时时间（单位：毫秒）
            policy.setConnectionTimeout(10000);  // 连接超时 10秒
            policy.setReceiveTimeout(10000);     // 读取接收超时 30秒

            // 应用策略
            conduit.setClient(policy);
        }
    }
}
